﻿<style type="text/css">
    .auto-style1 {
        width: 400px;
    }

    .easyui-textbox {
        width: 430px;
    }

    .auto-style2 {
        width: 150px;
    }

    .easyui-combobox {
        width: 430px;
    }
</style>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <title>部门管理</title>
    <link href="~/css/css_1.css" rel="stylesheet" />
    <link rel="stylesheet" type="text/css" href="../../easyUI/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="../../easyUI/themes/icon.css">
    <script type="text/javascript" src="../../easyUI/jquery.min.js"></script>
    <script type="text/javascript" src="../../easyUI/jquery.easyui.min.js"></script>
    <script>
        $(function () {
            jiazai();
        })

        function jiazai() {
            $.ajax({
                url: "http://localhost:25744/api/Department",
                type: "get",
                dataType: "json",
                success: function (data1) {
                    var msg = eval(data1);
                    $('#bumen').datagrid('loadData', msg);
                }
            })
        }
       //," + row.department_Id + ", '" + row.department_Name + "'
        function formatOper(val, row, index) {
            var s = "<a href='#' onclick='editUser(" + row.department_Id + ",\"" + row.department_Name + "\",\"" + row.department_Remarks + "\")'>修改</a> <a href='#' onclick='delUser(" + row.department_Id + ")'>删除</a>";
        
            return s;
        }
        //function editUser(row) {
        //    $('#w').window('open');
        //    $('#dg').datagrid('selectRow', index);// 关键在这里
        //    var row = $('#dg').datagrid('getSelected');
        //    if (row) {
        //        alert(1);
        //    }
        //}
        function editUser(q, w, e) {
            $('#w').window('open');
            $("#department_Id").textbox("setValue", q);
            $("#department_Name").textbox("setValue", w);
            $("#department_Remarks").textbox("setValue", e);
            $("#tian").hide();
            $("#xiu").show();

        }
       
        function tian()
        {
            $("#tian").show();
            $("#xiu").hide();
            $('#w').window('open');
        }

        function delUser(id) {
            if(confirm('确定删除吗'))
            {
                $.ajax({
                    url: "http://localhost:25744/apis/Department/Delete?id=" + id,
                    type: "Delete",
                    success: function (obj) {
                        if (obj > 0) {
                            alert('删除成功');
                            jiazai();
                        }
                        else {
                            alert("删除失败");
                        }
                    }
                })
            }
        }


    </script>
</head>
<body class="bb1">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="tian()">添加部门</a>
    <input class="easyui-textbox" type="text" name="loan_IdCard" id="loan_IdCard" >
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px">查询</a><br />
    <table class="easyui-datagrid" title="部门显示" data-options="singleSelect:true,collapsible:true" style="width:80%;height:70%" id="bumen">
        <thead>
            <tr>
                <th data-options="field:'department_Id',width:80">部门编号</th>
                <th data-options="field:'department_Name',width:80">部门名称</th>
                <th data-options="field:'department_Remarks',width:80">备注</th>
                <th data-options="field:'department_IsState',width:80">是否启用</th>
                <th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>
            </tr>
        </thead>
</table>
    <div id="w" class="easyui-window" title="部门信息" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:600px;height:200px;padding:10px;">
        <form>
            <table>
                <tr hidden="hidden" >
                    <td>部门编号</td>
                    <td>
                        <input id="department_Id" class="easyui-textbox" type="text" name="name" >
                    </td>
                </tr>
                <tr>
                    <td>部门名称</td>
                    <td>
                        <input id="department_Name" class="easyui-textbox" type="text" name="name" data-options="required:true">
                    </td>
                </tr>
                <tr>
                    <td>备注</td>
                    <td>
                        <input id="department_Remarks" class="easyui-textbox" type="text" name="name">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="add()" id="tian">添加</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="upt()" id="xiu" hidden="hidden">修改</a>
                        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#w').window('close')">取消</a>
                    </td>
                </tr>
            </table>
        </form>

    </div>
</body>
</html>
<script>

    function add() {
        var department_Name = $("#department_Name").val();
        var department_Remarks = $("#department_Remarks").val();
        $.ajax({
            url: "http://localhost:25744/api/Department",
            type: "post",
            data: { department_Name: department_Name, department_Remarks: department_Remarks },
            success: function (obj) {
                if (obj > 0) {
                    alert('添加成功');
                    $('#w').window('close');
                    jiazai();
                }
                else {
                    alert("添加失败");
                }
            }
        })

    }
    function upt() {
        var department_Id = $("#department_Id").val();
        var department_Name = $("#department_Name").val();
        var department_Remarks = $("#department_Remarks").val();
        $.ajax({
            url: "http://localhost:25744/api/Department",
            type: "Put",
            data: { department_Id:department_Id,department_Name: department_Name, department_Remarks: department_Remarks },
            success: function (obj) {
                if (obj > 0) {
                    alert('修改成功');
                    $('#w').window('close');
                    jiazai();
                }
                else {
                    alert("修改失败");
                }
            }
        })

    }
</script>